package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.mapper.StationMapper;
import com.indusfo.spc.mapper.StatisticalMapper;
import com.indusfo.spc.pojo.Order;
import com.indusfo.spc.pojo.OrderDete;
import com.indusfo.spc.pojo.Statistical;
import com.indusfo.spc.service.StationService;
import com.indusfo.spc.service.StatisticalService;
import com.indusfo.spc.vo.DateUtil;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 熊冰
 * @Date: 2020/3/23 14:25
 */
@Service("statisticalService")
public class StatisticalServiceImpl implements StatisticalService {

	private static final Logger logger = LoggerFactory.getLogger(StatisticalServiceImpl.class);

	@Resource
	private StatisticalMapper statisticalMapper;

	@Override
	public JSONObject querySevenFinishNum(Statistical statistical) {
		try {

			//执行查询操作
			List<Statistical> statisticalList = statisticalMapper.querySevenFinishNum(statistical);

			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, statisticalList, statisticalList.size());

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}

	@Override
	public JSONObject querySevenBadNum(Statistical statistical) {
		try {

			//执行查询操作
			List<Statistical> statisticalList = statisticalMapper.querySevenBadNum(statistical);

			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, statisticalList, statisticalList.size());

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}

	@Override
	public JSONObject queryOrderFinishRate(Order order) {
		try {

			//获取页面的数据个数
			Integer pagesize = order.getPagesize();
			//获取查询的页数
			Integer pageindex = order.getPageindex();
			if (pagesize != null && pageindex != null) {
				order.setIncept(pagesize * (pageindex - 1));
			}

			Statistical statistical=new Statistical();
			DateUtil dateUtil=new DateUtil();
			statistical.setDateStateId(1);
			if(order.getDateStateId()!=null){
				statistical.setDateStateId(order.getDateStateId());
			}
			statistical=dateUtil.getDate(statistical);
			order.setStartDate(statistical.getStartDate());
			order.setEndDate(statistical.getEndDate());


			//执行查询操作
			List<Order> orderList = statisticalMapper.queryOrderFinishRate(order);

			//如果页数和页面数量都不为空的情况下进行计数

			if (orderList.isEmpty()) {
				return JSONObject.oK(HintMessage.QUERY_NULL, orderList, 0);
			} else {
				Integer count = statisticalMapper.countOrderFinishRate(order);
				return JSONObject.oK(HintMessage.QUERY_SUCCESS, orderList, count);
			}

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject queryYearOrderRate(Statistical statistical) {
		try {

			//获取日期
			DateUtil dateUtil=new DateUtil();
			statistical=dateUtil.getDate(statistical);


			//执行查询操作
			List<Statistical> statisticalList = statisticalMapper.queryYearOrderRate(statistical);

			Integer count = statisticalMapper.countYearOrderRate(statistical);
			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, statisticalList, count);

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject queryCountFinishBatch(Statistical statistical) {
		try {
			//执行查询操作
			Integer b=0;
			DateUtil dateUtil=new DateUtil();
			statistical=dateUtil.getDate(statistical);
			Integer count = statisticalMapper.queryCountFinishBatch(statistical);
			statistical=dateUtil.getLastDate(statistical);
			Integer count_last = statisticalMapper.queryCountFinishBatch(statistical);
			if(count>count_last){
				b=1;
			}

			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, count,b);
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject queryCountFinishOrder(Statistical statistical) {
		try {
			//执行查询操作
			Integer b=0;
			DateUtil dateUtil=new DateUtil();
			statistical=dateUtil.getDate(statistical);
			Integer count = statisticalMapper.queryCountFinishOrder(statistical);
			statistical=dateUtil.getLastDate(statistical);
			Integer count_last = statisticalMapper.queryCountFinishOrder(statistical);
			if(count>count_last){
				b=1;
			}
			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, count,b);
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject queryWorkshopFinish(Statistical statistical) {
		try {
			//执行查询操作
			DateUtil dateUtil=new DateUtil();
			statistical=dateUtil.getDate(statistical);
			List<Statistical> statisticalList = statisticalMapper.queryWorkshopFinish(statistical);
			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, statisticalList,statisticalList.size());

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject queryCountFinishBatchAndOrder(Statistical statistical) {
		try {
			//执行查询操作
			Integer batch_b=0;
			Integer order_b=0;
			DateUtil dateUtil=new DateUtil();
			statistical=dateUtil.getDate(statistical);
			Integer batch_count = statisticalMapper.queryCountFinishBatch(statistical);
			Integer order_count = statisticalMapper.queryCountFinishOrder(statistical);

			statistical=dateUtil.getLastDate(statistical);
			Integer batch_count_last = statisticalMapper.queryCountFinishBatch(statistical);
			Integer order_count_last = statisticalMapper.queryCountFinishOrder(statistical);
			if(batch_count>batch_count_last){
				batch_b=1;
			}
			if(order_count>order_count_last){
				order_b=1;
			}
			List<Statistical> statisticalList =new ArrayList<>();

			Statistical statistical1=new Statistical();
			statistical1.setStatisticalNum(batch_count);
			statistical1.setStatisticalName(batch_b+"");
			Statistical statistical2=new Statistical();
			statistical2.setStatisticalNum(order_count);
			statistical2.setStatisticalName(order_b+"");
			statisticalList.add(statistical1);
			statisticalList.add(statistical2);
			//如果页数和页面数量都不为空的情况下进行计数
			return JSONObject.oK(HintMessage.QUERY_SUCCESS, statisticalList,statisticalList.size());
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		} catch (ParseException e) {
			e.printStackTrace();
			return JSONObject.build(500, e.getMessage());
		}
	}


}
